<?php
namespace app\admin\controller;
use \think\Request;
use app\common\model\Report as ReportModel;
use \think\Db;
class Report extends \app\admin\controller\Base {
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list'),
    );

    public function index(){
        $ReportModel = new ReportModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $list = $ReportModel->relData(15,$search);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,
                'report_business'=>$ReportModel::$report_business
            )
        );
        return $this->fetch();
    }

    /**
     * @return mixed|\think\response\Json
     * 举报处理
     */
    public function examine(){
        $ReportModel = new ReportModel();
        if(Request::instance()->isPost()){
            $id = input('post.id');
            $reason_name = input('post.reason');
            if($id!=''){//证明是编辑
                    $whereMap['report_id'] = $id;
                    $data['report_content'] =$reason_name;
                    $data['report_status'] = 2;
                    $data['report_handle'] = session('admin')['id'];
                    $res = $ReportModel->where($whereMap)->update($data);
                if ($res) {
                    return $this->api_success("操作成功");
                } else {
                    return $this->api_dbError("操作失败");
                }
            }

        }
        $id = input('get.id');
        $reason = Db::table('up_report')->where('report_id', $id)->value('report_reason');
//        if($id!=''){
//            $where['reason_id'] = $id;
//            $list = $AdoptModel->getOne($where);
        $this->assign('id', $id);
        $this->assign('reason', $reason);
//            $this->assign('ad', $list);
//        }
        return $this->fetch();
    }

    public function detail(){
        $id = input('get.id');
        $where['a.report_id'] = $id;
        $res = Db::table('up_report')->alias('a')
                              ->join('up_u_user uuu','a.report_user_id = uuu.user_id','left')
                              ->join('up_u_user uuu1','a.report_cover_user_id = uuu1.user_id','left')
                              ->join('up_admin ua','ua.id = a.report_handle','left')
                              ->join('up_report_name urn','urn.report_type_id = a.report_type','left')
                              ->join('up_dog ud','ud.dog_id = a.report_dog_id','left')
                              ->join('up_dog_family udf', 'udf.udf_id = ud.udf_id', 'left')
                              ->join('up_area ua1', 'ua1.district_id = ud.area_sheng', 'left')
                              ->join('up_area ua2', 'ua2.district_id = ud.area_shi', 'left')
                              ->join('up_area ua3', 'ua3.district_id = ud.area_qu', 'left')
                              ->where($where)
                              ->field('a.*,ua.nickname as adminname,urn.report_name,
                              uuu.user_username as username,uuu.user_attestation,uuu.user_rel_name,
                              uuu1.user_username,uuu1.user_attestation as user_attestation1,uuu1.user_rel_name as user_rel_name1
                              ,ud.dog_img,ud.dog_type,ud.dog_information,ud.dog_adpot_information,ud.dog_num,ud.udsc_id,ud.udsc_id_information,ud.dog_help_style,ud.dog_help_information,ud.dog_end_time,
                              ua1.district as province, ua2.district as city,ua3.district as district,
                              udf.udf_name')
                              ->find();
        if($res['dog_img']!=''){
            $img = rtrim($res['dog_img'],',');
            $res['img'] = explode(',',$img);
        }else{
            $res['img'] = array();
        }
        if($res['report_business']==1){
            $res['type'] = '流浪狗';
//            //流浪狗特征
            $whereS['udsc_id'] = array('IN', trim($res['udsc_id'], ','));
            $udsc_name = Db::name('dog_strat_characteristic')->where($whereS)->column('udsc_name');
            if($udsc_name){
                $res['udsc_id_information'] = implode(',', $udsc_name) .  $res['udsc_id_information'];
            }
        }elseif ($res['report_business']==2){
            $res['type'] = '互助';
            if($res['dog_end_time'] != ''){
                $res['dog_end_time'] = date('Y-m-d', strtotime($res['dog_end_time']));
            }
            $whereH['heo_id'] = array('IN', trim($res['dog_help_style']), ',');
            $heo_name = Db::name('dog_heo')->where($whereH)->column('heo_name');
            if($heo_name){
                $res['dog_help_information'] = implode(',', $heo_name) . $res['dog_help_information'];
            }
        }elseif ($res['report_business']==3){
            $res['type'] = '领养';
        }else{
            $res['type'] = '寻狗';
        }
        $this->assign('res',$res);
        return $this->fetch();
    }
    
    public function cover(){
        $ReportModel = new ReportModel();
        $user_id = input('id');
        $page = input('page');
        $list = Db::name('report')
            ->alias('r')
            ->join('up_u_user uuu', 'uuu.user_id = r.report_user_id', 'left')
            ->join('up_report_name urn', 'urn.report_type_id = r.report_type', 'left')
            ->join('up_admin ua', 'ua.id = r.report_handle', 'left')
            ->field('r.report_business, uuu.user_rel_name, uuu.user_phone, r.report_time, urn.report_name, ua.nickname, r.report_start_time')
            ->where('report_cover_user_id', $user_id)
            ->order('report_time desc')
            ->paginate($page);
        $this->assign(array(
            'list'=>$list,
            'report_business'=>$ReportModel::$report_business
        ));
        return $this->fetch();
    }
}